﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SETUPABON.xaml
    /// </summary>
    public partial class ITXSETUP : DocumentBase
    {
        ApplicationBase ApplicationBase;
        bool IsSetUp = false;

        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        #region=====vwFUNCTION
        CollectionViewSource vwFUNCTION
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFUNCTION");
            }
        }
        DataRow dwFUNCTION
        {
            get
            {
                if (vwFUNCTION.View == null)
                    return null;
                if ((DataRowView)vwFUNCTION.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFUNCTION.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwICC_FUNCTIONS
        CollectionViewSource vwICC_FUNCTIONS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwICC_FUNCTIONS");
            }
        }
        DataRow dwEFUNCTION
        {
            get
            {
                if (vwICC_FUNCTIONS.View == null)
                    return null;
                if ((DataRowView)vwICC_FUNCTIONS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwICC_FUNCTIONS.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwTMPFUNCTION
        CollectionViewSource vwTMPFUNCTION
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPFUNCTION");
            }
        }
        DataRow dwTMPFUNCTION
        {
            get
            {
                if (vwTMPFUNCTION.View == null)
                    return null;
                if ((DataRowView)vwTMPFUNCTION.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTMPFUNCTION.View.CurrentItem).Row;
            }
        }
        #endregion

        #region=====vwFOLDER=======
        CollectionViewSource vwFOLDER
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFOLDER");
            }
        }
        DataRow dwFOLDER
        {
            get
            {
                if (vwFOLDER.View == null)
                    return null;
                if ((DataRowView)vwFOLDER.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFOLDER.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwMENU=======
        CollectionViewSource vwMENU
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMENU");
            }
        }
        DataRow dwMENU
        {
            get
            {
                if (vwMENU.View == null)
                    return null;
                if (SetData.ICC_MENUDEF.Rows.Count == 0)
                    return null;
                if ((DataRowView)vwMENU.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMENU.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwTMPMENU=======
        CollectionViewSource vwTMPMENU
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPMENU");
            }
        }
        DataRow dwTMPMENU
        {
            get
            {
                if (vwTMPMENU.View == null)
                    return null;
                if ((DataRowView)vwTMPMENU.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTMPMENU.View.CurrentItem).Row;
            }
        }
        #endregion
        BindingListCollectionView cvDlgMenu
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgMenu.ItemsSource);
            }
        }
        DataSet setDISPO = new DataSet();
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public ExcelX.ExcelLib.ExcelAgent exAgent;
        ITXSETUPData m_Data;
        public string Mandant
        {
            get
            {
                return (string)dicParamater["MANDANT"];
            }
        }
        public string company
        {
            get
            {
                return (string)dicParamater["Company"];
            }
        }
        public ITXSETUP()
        {
            InitializeComponent();
            this.Name = "参数设定";
            this.Title = "参数设定";
            exAgent = new ExcelX.ExcelLib.ExcelAgent("");
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (!IsSetUp)
            {
                ApplicationBase.Quit();
            }
            base.OnClosing(e); 
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsSetUp)
                {
                    ApplicationBase = new ApplicationBase();
                    ApplicationBase.Load();
                    dicParamater = ApplicationBase.dicParamater;
                }
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                m_Data = new ITXSETUPData(OracleDB, proxy);
                SetEnv.Merge(m_Data.LoadEnv(Mandant));
                vwMENU.View.CurrentChanged += vwMENU_CurrentChanged;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        void vwMENU_CurrentChanged(object sender, EventArgs e)
        {
            if (dwMENU == null)
                return;
            txtCurrentUser.Text = dwMENU["USERID"].ToString();
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                    this.dicParamater = dicParamater;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnGetCADMap_Click(object sender, RoutedEventArgs e)
        {
            DataSet setresult = null;
            try
            {
                SetData.Clear();

                setresult = m_Data.ReSetData();
                SetData.Merge(setresult);
                SetData.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetData.Merge(setresult);
                SetData.AcceptChanges();
            }
        }

        private void butFunctionITXLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                setResult = m_Data.LoadITXFunction(Mandant);
                SetData.Clear();
                SetData.Merge(setResult, false, MissingSchemaAction.Ignore);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetData.Clear();
                SetData.Merge(setResult, false, MissingSchemaAction.Ignore);
                SetData.AcceptChanges();
            }
        }
        private void butFunctionLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                setResult = m_Data.LoadFunction();
                SetData.Clear();
                SetData.Merge(setResult, false, MissingSchemaAction.Ignore);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetData.Clear();
                SetData.Merge(setResult, false, MissingSchemaAction.Ignore);
                SetData.AcceptChanges();
            }
        }
        private void butFunctionCopy_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                if (dwFUNCTION == null)
                    return;
                DataRow dwNewFolder = SetProd.ICC_FUNCTIONS.NewICC_FUNCTIONSRow();
                dwNewFolder.ItemArray = dwFUNCTION.ItemArray;
                dwNewFolder["ROWNID"] = "0";
                SetProd.ICC_FUNCTIONS.Rows.Add(dwNewFolder);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butFunctionEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetProd.Clear();
                SetProd.Merge(SetData.ICC_FUNCTIONS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
            finally
            {

            }
        }
        private void butFunctionCopyAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetProd.Clear();
                SetProd.Merge(SetData.ICC_FUNCTIONS);
                foreach (DataRow RX in SetProd.ICC_FUNCTIONS.Rows)
                {
                    RX["ROWNID"] = "0";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }

        }
        private void butFunctionSave_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                m_Data.SaveFunctionSetup(SetProd, "00");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butFolderITXLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                setResult = m_Data.LoadITXFolder(Mandant);
                SetData.Clear();
                SetData.Merge(setResult, false, MissingSchemaAction.Ignore);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetData.Clear();
                SetData.Merge(setResult, false, MissingSchemaAction.Ignore);
                SetData.AcceptChanges();
            }
        }
        private void butFolderLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                setResult = m_Data.LoadFolder(Mandant);
                SetData.Clear();
                SetData.Merge(setResult, false, MissingSchemaAction.Ignore);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetData.Clear();
                SetData.Merge(setResult, false, MissingSchemaAction.Ignore);
                SetData.AcceptChanges();
            }
        }
        private void butFolderCopy_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                if (dwFOLDER == null)
                    return;
                DataRow dwNewFolder = SetProd.ICC_FOLDERS.NewICC_FOLDERSRow();
                dwNewFolder.ItemArray = dwFOLDER.ItemArray;
                dwNewFolder["ROWNID"] = "0";
                SetProd.ICC_FOLDERS.Rows.Add(dwNewFolder);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butFolderSave_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                m_Data.SaveFolderSetup(SetProd, "00");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        private void butFolderCopyAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetProd.Clear();
                SetProd.Merge(SetData.ICC_FOLDERS);
                foreach (DataRow RX in SetProd.ICC_FOLDERS.Rows)
                {
                    RX["ROWNID"] = "0";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
            finally
            {

            }
        }
        private void butMenuITXLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                setResult = m_Data.LoadITXMenu(Mandant, company);
                SetData.Clear();
                SetData.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetData.Clear();
                SetData.Merge(setResult);
                SetData.AcceptChanges();
            }
        }
        private void butMenuLoad_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                setResult = m_Data.LoadMenu();
                SetData.Clear();
                SetData.Merge(setResult, false, MissingSchemaAction.Ignore);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetData.Clear();
                SetData.Merge(setResult, false, MissingSchemaAction.Ignore);
                SetData.AcceptChanges();
            }
        }
        private void butMenuSave_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                m_Data.SaveMenuSetup(SetProd, "00");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butNewMenu_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                if (dwMENU == null)
                    return;
                DataRow dwNewMenu = SetProd.ICC_MENUDEF.NewICC_MENUDEFRow();
                dwNewMenu.ItemArray = dwMENU.ItemArray;
                dwNewMenu["ROWNID"] = "0";
                SetProd.ICC_MENUDEF.Rows.Add(dwNewMenu);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butMenuCopy_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                string filter = "USERID='" + txtCurrentUser.Text.ToString() + "'";
                DataRow[] rows = SetData.ICC_MENUDEF.Select(filter);
                DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ICC_MENUDEF"; ;
                foreach (DataRow RX in tb.Rows)
                {
                    RX["USERID"] = txtNewUser.Text.ToString();
                    RX["ROWNID"] = "0";
                }
                SetProd.Clear();
                SetProd.Merge(tb);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butMenuEdit_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                string filter = "USERID='" + txtCurrentUser.Text.ToString() + "'";
                DataRow[] rows = SetData.ICC_MENUDEF.Select(filter);
                DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ICC_MENUDEF"; ;
                SetProd.Clear();
                SetProd.Merge(tb);


            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                DataRow[] rows;
                string USERID = txtCurrentUser.Text;
                rows = SetData.ICC_MENUDEF.Select("USERID = '" + USERID + "'");
                foreach (DataRow rw in rows)
                {
                    rw.Delete();
                }
                SetData.AcceptChanges();
            }
        }

        private void butDiscard_Click(object sender, RoutedEventArgs e)
        {
            SetProd.Clear();
            SetProd.AcceptChanges();
        }
        private void butMenuCopyAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetProd.Clear();
                SetProd.Merge(SetData.ICC_MENUDEF);
                foreach (DataRow RX in SetProd.ICC_MENUDEF.Rows)
                {
                    RX["ROWNID"] = "0";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
            finally
            {
                SetProd.Clear();
                SetProd.Merge(SetData.ICC_MENUDEF);
            }
        }

        private void txtFunctionType_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (dwTMPFUNCTION == null)
                    return;
                string tag = txtType.Text.Trim();
                dwTMPFUNCTION["NAMESPACE"] = "WPFExcelReport";
                dwTMPFUNCTION["TYPE"] = tag;
                dwTMPFUNCTION["FULLNAME"] = tag;
                dwTMPFUNCTION["FILENAME"] = tag + ".dll";
            }
        }
        private void btnFilterMENU_Click(object sender, RoutedEventArgs e)
        {
            string filter = "USERID ='" + txtCurrentUser.Text + "'";
            cvDlgMenu.CustomFilter = filter;
        }
        private void btnFilterMenuClear_Click(object sender, RoutedEventArgs e)
        {
            cvDlgMenu.CustomFilter = null;
        }
        private void btnFilterMenuHidden_Click(object sender, RoutedEventArgs e)
        {
            m_Data.DeleteHiddenMenu();
        }
        private void cmbFunction_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (dwEFUNCTION == null)
                return;
            if (dwTMPMENU == null)
                return;
            dwTMPMENU["HEADER"] = dwEFUNCTION["DESCRIPTION"].ToString().Trim();
            dwTMPMENU["MENUITEM"] = "cmd" + dwTMPMENU["TAG"].ToString().Trim();
        }
    }
}
